package com.lagou.handler;

import com.lagou.request.RpcRequest;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.Callable;

/**
 * @author xsl
 * @date 2021/3/13 15:15
 * @Description: UserClientHandler
 */
public class UserClientHandler extends ChannelInboundHandlerAdapter implements Callable {

    /**
     * 定义成员变量
     */
    private ChannelHandlerContext ctx;
    private String result = "";
    private RpcRequest request;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //初始化通道处理器上下文
        this.ctx = ctx;
    }

    @Override
    public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        this.result = "success";
        notify();
    }

    @Override
    public synchronized Object call() throws Exception {
        //context向服务端写数据
        this.ctx.writeAndFlush(this.request);
        wait();
        return this.result;
    }

    public void setRpcRequest(RpcRequest request) {
        this.request = request;
    }
}
